/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jsrpc.core;

import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;

/**
 *
 * @author neo
 */
public class DefaultObjectHandler extends ObjectHandler {

    @Override
    public Object parseObject(ParserObject parser, Hashtable table, Properties prop) {
        setProperties(prop);
        String loadClass = ((String) table.get(Key.JAVA_CLASS)).trim();
        if (loadClass.startsWith("\"") && loadClass.endsWith("\"")) {
            loadClass = loadClass.substring(1, loadClass.length() - 1);
        }
        table.remove(Key.JAVA_CLASS);
        //System.out.println("load Class[" + loadClass + "]");
        try {
            Object obj = Class.forName(loadClass).newInstance();
            for (Enumeration enums = table.keys(); enums.hasMoreElements();) {
                String methodName = (String) enums.nextElement();
                String value = (String) table.get(methodName);
                //System.out.println("isType:"+ isType(value));
            //System.out.println("isValue["+ value+"]");
                String nName = toGetMethodName(methodName);

                Method mref = obj.getClass().getDeclaredMethod(nName, new Class[]{});
//System.out.println("isType:"+ isType(mref.getReturnType()));
//            System.out.println("");
//            System.out.println("");
            //System.out.println("["+ toSetMethodName(methodName) +"]");
                //switch (isType(value)) {
                switch( isType(mref.getReturnType()) ) {
                    case 0:
                        this.setString(obj, toSetMethodName(methodName), value);
                        break;
                    case 1:
                        this.setInt(obj, toSetMethodName(methodName), value);
                        break;
                    case 2:
                        this.setFloat(obj, toSetMethodName(methodName), value);
                        break;
                    case 3:
                        this.setDate(obj, toSetMethodName(methodName), value);
                        break;
                    case 4:
                        this.setBoolean(obj, toSetMethodName(methodName), value);
                        break;
                    case 5:
                        this.setInteger(obj, toSetMethodName(methodName), value);
                        break;    
                }
            }
            return obj;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

}
